﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using McsUtility;
using log4net;

namespace MinerManager.Forms
{
	public partial class UserLogin : Form
	{
		ILog logger = LogManager.GetLogger(typeof(Program));

		public event EventHandler<LoginHandleEventArgs> OnHandle;
		public event EventHandler<EventArgs> OnCancel;
		
		public UserLogin()
		{
			InitializeComponent();
		}

		#region 窗体事件

		private void UserLogin_Load(object sender, EventArgs e)
		{
			if (System.IO.File.Exists(Common.Utility.AutoLoginPath))
			{
				string autoLoginString = System.IO.File.ReadAllText(Common.Utility.AutoLoginPath, Encoding.UTF8);
				if (!string.IsNullOrWhiteSpace(autoLoginString))
				{
					ItemUserName.Text = autoLoginString.Split(',')[0];
					ItemPassword.Text = autoLoginString.Split(',')[1];
					ItemAutoLogin.Checked = true;
				}
			}
		}

		private void UserLogin_FormClosing(object sender, FormClosingEventArgs e)
		{

		}

		#endregion

		#region void BtnLogin_Click(object sender, EventArgs e)

		private void BtnLogin_Click(object sender, EventArgs e)
		{
			//非法信息检测
			if (string.IsNullOrWhiteSpace(ItemUserName.Text.Trim()))
			{
				if (MessageBox.Show("请输入登录账号。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
				{
					ItemUserName.Focus();
					ItemUserName.SelectAll();
				}
				return;
			}

			if (!ItemUserName.Text.IsMobile())
			{
				if (MessageBox.Show("请输入正确的登录账号。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
				{
					ItemUserName.Focus();
					ItemUserName.SelectAll();
				}
				return;
			}

			if (string.IsNullOrWhiteSpace(ItemPassword.Text.Trim()))
			{
				if (MessageBox.Show("请输入登录密码。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
				{
					ItemPassword.Focus();
					ItemPassword.SelectAll();
				}
				return;
			}

			//自动登录
			if (ItemAutoLogin.Checked)
			{
				System.IO.File.WriteAllText(Common.Utility.AutoLoginPath, ItemUserName.Text.Trim() + "," + ItemPassword.Text.Trim(), Encoding.UTF8);
			}
			else
			{
				System.IO.File.Delete(Common.Utility.AutoLoginPath);
			}

			//执行操作
			OnHandle?.Invoke(this, new LoginHandleEventArgs(ItemUserName.Text.Trim(), ItemPassword.Text.Trim()));
		}

		#endregion

		#region void BtnClose_Click(object sender, EventArgs e)

		private void BtnClose_Click(object sender, EventArgs e)
		{
			OnCancel?.Invoke(this, new EventArgs());
			this.Close();
		}

		#endregion

		#region public void HandleFeedback(bool isSuccess) 操作结果反馈。

		private delegate void DelegateHandleFeedback(bool isSuccess);

		/// <summary>
		/// 操作结果反馈。
		/// </summary>
		/// <param name="isSuccess">是否操作成功</param>
		public void HandleFeedback(bool isSuccess)
		{
			if (this.InvokeRequired)
			{
				DelegateHandleFeedback d = new DelegateHandleFeedback(HandleFeedback);
				this.BeginInvoke(d, isSuccess);
			}
			else
			{
				if (isSuccess)
				{
					OnCancel?.Invoke(this, new EventArgs());
					this.Close();
				}
				else
				{
					MessageBox.Show("登录失败，请检查登录账号和密码是否正确。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		#endregion



		#region void TextBox_KeyPress(object sender, KeyPressEventArgs e)

		private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == 13)
			{
				e.Handled = true;
				BtnLogin_Click(BtnLogin, new EventArgs());
			}

			if (e.KeyChar == 27)
			{
				BtnClose_Click(BtnClose, new EventArgs());
			}
		}

		#endregion
	}
}
